//
//  PEEfficiencyCircleView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/11.
//

#import "RHEfficiencyCircleView.h"
@interface RHEfficiencyCircleView()
@property (nonatomic ,strong) QMUILabel *percentLab;

@property (nonatomic ,strong) QMUILabel *titleLab;
@end
@implementation RHEfficiencyCircleView

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setPercent:(CGFloat)percent
{
    _percent = percent;
    self.percentLab.text = [NSString stringWithFormat:@"%ld%%",(NSInteger)(percent*100)];
    [self setNeedsDisplay];
}

-(void)setupUI{
    self.percent = 0.1;
    self.backgroundColor = [UIColor clearColor];
    _percentLab = [[QMUILabel alloc]init];
    _percentLab.text = @"80%";
    _percentLab.textColor = kCOLOR_HEX(@"3F4443");
    _percentLab.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:_percentLab];
    
    _titleLab = [[QMUILabel alloc]init];
    _titleLab.text = @"能效";
    _titleLab.textColor = kCOLOR_HEX(@"3F4443");
    _titleLab.font = [UIFont systemFontOfSize:13];
    [self addSubview:_titleLab];
    
    [_percentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(-10);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(10);
    }];
}
-(void)layoutSubviews
{
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGPoint center = CGPointMake(self.width/2, self.height/2);
    CGFloat  bigLineWidth = 16;
    CGFloat  smallLineWidth = 16;;
    CGFloat  bigRadius = MIN(self.width, self.height)/2 - bigLineWidth/2;
    CGFloat  smallRadius = bigRadius;
    
    UIBezierPath *totalPath = [UIBezierPath bezierPathWithArcCenter:center radius:smallRadius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    totalPath.lineWidth = smallLineWidth;
    [[UIColor colorWithHexString:@"56C271" alpha:0.3] setStroke];
    [totalPath stroke];
    
    if(self.percent == 0)return;
    CGFloat random = arc4random_uniform(360);
    CGFloat startAngle = random * M_PI/180;
    CGFloat endAngle = startAngle + self.percent*2*M_PI;
    UIBezierPath *currentPath = [UIBezierPath bezierPathWithArcCenter:center radius:bigRadius startAngle:startAngle endAngle:endAngle clockwise:YES];
    currentPath.lineCapStyle = kCGLineCapRound;
    currentPath.lineWidth = bigLineWidth;
    [[UIColor colorWithHexString:@"56C271"] setStroke];
    [currentPath stroke];
    
    
}

@end
